Imports NUnit.Framework
Imports pixolut.BizBlox
Imports System.Globalization

<TestFixture()> Public Class DOCollectionLargeDSTests
    Public Sub New()
        MyBase.New()
    End Sub

    <TestFixtureSetUp()> Public Sub GlobalInit()
        Setup.CreateDataSet()
    End Sub

    <TestFixtureTearDown()> Public Sub GlobalDestruct()
        Setup.ClearDataSet()
    End Sub

    'should return 1218 names starting with the letter a
    '(see dictionary class)
    <Test()> Public Sub testSearch()
        Dim sCol As New StudentCollection
        sCol.SearchByStudentName("starts with a")
        Console.WriteLine("found: " & sCol.Count)
        Assert.AreEqual(1218, sCol.Count)
        sCol.Dispose()
    End Sub

    <Test()> Public Sub testRandomRepeatQueries()
        Dim sCol As New StudentCollection
        Dim r As New Random
        Dim ch As String
        For i As Integer = 0 To 100000
            ch = "starts with " & Chr(r.Next(65, 150))
            sCol.SearchByStudentName(ch)
            Console.WriteLine("""" & ch & """ found " & sCol.Count & " entries.")
        Next
        sCol.Dispose()
    End Sub

    <Test()> Public Sub testBulkRepeatQueries()
        Dim oCol As New LunchOrderCollection
        Dim fCol As New FoodCollection
        For j As Integer = 0 To 50000
            fCol.FindAll()
            For i As Integer = 0 To fCol.Count - 1
                oCol.FindByFood(fCol(i).FoodID)
                'Console.WriteLine("Bulk Find " & fCol(i).FoodID.ToString() & ": " & oCol.Count & " entries.")
            Next
            Console.Write("*")
            If j Mod 50 = 0 Then Console.WriteLine()
            Console.Out.Flush()
        Next
        fCol.Dispose()
        oCol.Dispose()
    End Sub

    <Test()> Public Sub testBulkRepeatQueriesNoDisposal()
        Dim oCol As New LunchOrderCollection
        Dim fCol As New FoodCollection
        For j As Integer = 0 To 50000
            fCol = New FoodCollection
            fCol.FindAll()
            For i As Integer = 0 To fCol.Count - 1
                oCol = New LunchOrderCollection
                oCol.FindByFood(fCol(i).FoodID)
                'Console.WriteLine("Bulk Find " & fCol(i).FoodID.ToString() & ": " & oCol.Count & " entries.")
            Next
            Console.Write("*")
            If j Mod 50 = 0 Then Console.WriteLine()
            Console.Out.Flush()
        Next
    End Sub


    <Test()> Public Sub TestPreCount()
        Dim sCol As New StudentCollection
        Dim mySQl As SQLBuilder = sCol.GetSqlBuilder
        sCol.SetCurrentQuery(mySQl)
        Dim count As Integer = sCol.Count
        Assert.AreEqual(5001, sCol.Count, "Pre Count")
    End Sub

    <Test()> Public Sub testFindRange()
        Dim sCol As New StudentCollection
        Dim mySql As SQLBuilder = sCol.GetSqlBuilder
        mySql.addSelect(Student.Columns.StudentID)
        mySql.addOrderBy(Student.Columns.Name)
        sCol.SetCurrentQuery(mySql)
        Dim count As Integer = sCol.Count
        sCol.FindRange(0, 99)
        Assert.AreEqual(100, sCol.Count, "Find Range Count")
        Dim sObj As Student = sCol(0)
        Dim firstName As String = sObj.Name.Split()(0)
        Assert.AreEqual("Aachen", firstName, "Object at Start Index")
    End Sub

    <Test()> Public Sub testFindMiddleRange()
        Dim sCol As New StudentCollection
        Dim mySql As SQLBuilder = sCol.GetSqlBuilder
        mySql.addSelect(Student.Columns.StudentID)
        mySql.addOrderBy(Student.Columns.Name)
        sCol.SetCurrentQuery(mySql)
        Dim count As Integer = sCol.Count
        sCol.FindRange(10, 109)
        Assert.AreEqual(100, sCol.Count, "Find Range Count")
        Dim sObj As Student = sCol(0)
        Dim firstName As String = sObj.Name.Split()(0)
        Assert.AreEqual("Abampere", firstName, "Object at Start Index")
    End Sub

    <Test(), ExpectedException(GetType(IndexOutOfRangeException))> Public Sub testOutOfRange()
        Dim sCol As New StudentCollection
        Dim mySql As SQLBuilder = sCol.GetSqlBuilder
        mySql.addSelect(Student.Columns.StudentID)
        sCol.SetCurrentQuery(mySql)
        Dim count As Integer = sCol.Count
        sCol.FindRange(0, 49)
        Dim sObj As Student = sCol(50)
    End Sub



End Class
